package co.th.bjc.md.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class ConnectDB {
	
	private Connection connection;
    private static final String DEFAULT_CONFIG = "config";
	private static final ResourceBundle configBundle = ResourceBundle.getBundle(DEFAULT_CONFIG);
	private static final String URL = configBundle.getString("URL");
	private static final String DB = configBundle.getString("DB");
	private static final String DRIVER = configBundle.getString("DRIVER");
	private static final String USERNAME = configBundle.getString("USERNAME");
	private static final String PASSWORD = configBundle.getString("PASSWORD");
	private static final String CharacterEncoding = "?characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull";

	public Connection getConnection() throws Exception{
		try{			
			Class.forName(DRIVER).newInstance();
			connection = (Connection) DriverManager.getConnection(URL + DB + CharacterEncoding, USERNAME, PASSWORD);
		}catch(Exception ex ){
			ex.printStackTrace();
			connection = null;
		}
		return connection;
	}
	
	public void close(){
		if (connection != null){
			try {
				if (!connection.isClosed()){
					connection.close();
				}
				connection = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
